<template>
<div class="login" style="width: 1200px; ">
    <el-card style="width: 500px;margin: 50px auto;height: 100%!important;">
      <el-form label-width="80px" style="width: 400px;margin: 10px auto" :model="ruleForm":rules="rules" ref="ruleForm">
        <el-form-item style="margin-bottom: 10px">
          <a style="float: left;
                                text-decoration: none;color: #409EFF;
                                position: relative;top: 20px"  href="/reg">
            前往注册
          </a>
          <a style="float: right;
                                text-decoration: none;color: #409EFF;
                                position: relative;top: 20px"  href="/personal/password">
            忘记密码?找回密码
          </a>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input placeholder="请输入用户名" v-model="ruleForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password"  placeholder="请输入用户密码" v-model="ruleForm.password"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
</div>
</template>

<script>
export default {
  name: "LoginView",
  data() {
    return {
      ruleForm: {
        userName: '',
        password:'',

      },
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],

      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url= "http://localhost:8080/v1/users/login";
          console.log(""+url)
          let formData=this.qs.stringify(this.ruleForm)
          console.log(formData)
          this.axios.post(url,formData).then((response) => {
            if(response.data.code==1){
              this.$message({
                message:'登录成功',
                type:'success',
              },);
              let loginResult = response.data.data;
              localStorage.setItem('localJwt', loginResult.token);
              console.log(loginResult.token)
              localStorage.setItem('id', loginResult.id);
              localStorage.setItem('userName', loginResult.userName);
              localStorage.setItem('nickName', loginResult.nickName);
              localStorage.setItem('imgUrl', loginResult.imgUrl);
              localStorage.setItem('password',loginResult.password);
              console.log(loginResult.token)
              if(document.referrer.includes("/login")){
                location.href="/"
              }else{
                window.location.href= document.referrer
              }
            }else {
              this.$message.error(response.data.msg);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }

}
</script>

<style scoped>
.login{
background-image: url("http://localhost:9090/imgs/back.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  overflow:hidden;
}


</style>